--
-- Author: ping.wang
-- Date: 2019-04-30 16:15:32
--
-- NewRankModel
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)
local table_insert = table.insert
local NewguildInfo = require("app.newGuild.models.NewguildInfo") -- 公会基础数据结构
local GuildBaseInfo = require("app.public.dao.GuildBaseInfo")

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    -- self:readGuildRankConfig()
end

--获取公会排行配置
-- function ClassRef:readGuildRankConfig()
--     self._guildRankInfos = {}
--     --读取rank表中所有公会排行信息
--     local rankConfigs = GD:getRankingConfigs()
--     for k,v in ipairs(rankConfigs) do
--         local isGuild = checknumber(v.isguild)
--         if isGuild == 1 or isGuild == 2 then
--             
--         end
--     end

--     --sort by order
--     -- table.sort(self._guildRankInfos, function(a, b)
--     --     return checknumber(a.rankConfig.order) < checknumber(b.rankConfig.order)
--     -- end)
-- end

function ClassRef:getRankMenuData()
    local rankConfigs = GD:getRankingConfigs()
    local curServerMenu = {}
    local crossServerMenu = {}
    for k,v in ipairs(rankConfigs) do
        local isGuild = checknumber(v.isguild)
        if isGuild == 1 then
            table.insert( curServerMenu,  {rankConfig = v})
        elseif isGuild == 2 then
            table.insert( crossServerMenu,  {rankConfig = v})
        end
    end

    table.sort(curServerMenu, function(a, b)
        return checknumber(a.rankConfig.order) < checknumber(b.rankConfig.order)
    end)
    table.sort(crossServerMenu, function(a, b)
        return checknumber(a.rankConfig.order) < checknumber(b.rankConfig.order)
    end)
    return {
        {id = 1, name = L("rankGuild7"),subDatas = curServerMenu},
        {id = 2, name = L("rankGuild8"),subDatas = crossServerMenu}
    }
end




-- function ClassRef:getGuildRankConfig()
--     return self._guildRankInfos
-- end



--第二列 根绝subtitle来取
function ClassRef:getGuildRankSubContent(guildInfo, subtitle1)
    local ret = 0
    if subtitle1 == 6 then
        ret = guildInfo:getGuildMemberNum()
    elseif subtitle1 == 7 then
        ret = guildInfo:getHighestCombatPowr()
    elseif subtitle1 == 8 then
        ret = guildInfo:getGuildLv()
    elseif subtitle1 == 9 then
        ret = guildInfo:getOwnTownNum()
    end
    if ret > 10000 then
        return  string.format("%d%s",math.floor(ret/10000),L("damageunit1"))
    else
        return tostring(ret)
    end
end

--第三列 根据rankid来取
function ClassRef:getGuildRankListContent(guildInfo, rankId)
    local ret = 0
    if rankId == Enums.GuildRankType.GuildLv then
        ret = guildInfo:getGuildLv()
    elseif rankId == Enums.GuildRankType.TotalCombatPower then
        ret = guildInfo:getGuildTotalCombatPw()
    elseif rankId == Enums.GuildRankType.ActivityMemberNums then
        ret = guildInfo:getActiveMemberNum()
    elseif rankId == Enums.GuildRankType.TotalContributions then
        ret = guildInfo:getTotalContribution()
    elseif rankId == Enums.GuildRankType.TotalProsperity then
        ret = guildInfo:getTotalProsperity()
    end

    if ret > 10000 then
        return  string.format("%d%s",math.floor(ret/10000),L("damageunit1"))
    else
        return tostring(ret)
    end
end

function ClassRef:getValue1ByBaseGuildData(pbData, subtitle1)
    local ret = 0
    if subtitle1 == 6 then
        ret = rawget(pbData, "memberNum") or 0
    elseif subtitle1 == 7 then
        ret = rawget(pbData, "highestCombatPowr") or 0
    elseif subtitle1 == 8 then
        ret = rawget(pbData, "lv") or 0
    elseif subtitle1 == 9 then
        ret =  0
    end
    if ret > 10000 then
        return  string.format("%d%s",math.floor(ret/10000),L("damageunit1"))
    else
        return tostring(ret)
    end
end


------------------------ receive ------------------------
function ClassRef:requestGuildRankWithRankConfig(rankConfig)
    --dump(aID,"NewRankModel requestGuildRank")
    if not rankConfig then
        return
    end
    local rankType = checknumber(rankConfig.isguild)
    if rankType == 1 then
        app.session:request("pb_guild.C2S_GuildRankList", { rankId = checknumber(rankConfig.ID) }, function(msg)
            local serverGuildRankInfos = {}
            local rankInfos = {}
            local curRankConfig = rankConfig--GD:getRankingConfigById(msg.rankId)
            --[[
                rank
                guildIcon
                serverName
                guildName
                guildId
                value1
                value2
            ]]
            if msg.guildInfos then
                for k,v in ipairs(msg.guildInfos) do
                    local guildVo = NewguildInfo.new(v)
                    table_insert(rankInfos, {
                        rank = tonumber(k),
                        guildIcon = guildVo:getGuildIcon(),
                        guildName = guildVo:getGuildName(),
                        guildId = guildVo:getGuildId(),
                        value1 = self:getGuildRankSubContent(guildVo, tonumber(curRankConfig.Subtitle1)),
                        value2 = self:getGuildRankListContent(guildVo, msg.rankId),
                        isShowDetail = true,
                        guildInfo = guildVo
                    })
                end
            end
    
            serverGuildRankInfos.rankInfos = rankInfos
            serverGuildRankInfos.myGuildPaiming = msg.myGuildRank --未上榜=0 没公会也是0
            --通知去刷新公会排行界面
            app:sendMsg( "NewRankController", "refreshGuildRankView",serverGuildRankInfos)
        end, {block = true})
    elseif rankType == 2 then
        app.session:request("pb_rank.C2S_WorldRankList", { id = protobuf.enum_name("pb_rank.RANK_ID", checknumber(rankConfig.ID))  }, function(msg)
            local serverGuildRankInfos = {}
            local rankInfos = {}
            local nodes = rawget(msg, "nodes")
            local curRankConfig = rankConfig 
            if nodes then
                for k,v in ipairs(nodes) do
                    local baseGuild = rawget(v, "guild")
                    if baseGuild then
                        local value2 = rawget(v, "value") or 0
                        if value2 > 10000 then
                            value2 =  string.format("%d%s",math.floor(value2/10000),L("damageunit1"))
                        else
                            value2 = tostring(value2)
                        end

                        local serverName = ""
                        local server = rawget(v, "server")
                        if server then
                            serverName = app.channelAPI:getServerNameByIndex(server)
                        end
                        local guildInfo = GuildBaseInfo.new()
                        guildInfo:read(baseGuild)
                        table_insert(rankInfos, {
                            rank = tonumber(k),
                            guildIcon = guildInfo:getGuildIconData(),
                            guildName = guildInfo:getGuildName(),
                            guildId = guildInfo:getGuildId(),
                            value1 = self:getValue1ByBaseGuildData(baseGuild, tonumber(curRankConfig.Subtitle1)),
                            value2 = value2,
                            serverName = serverName,
                            guildInfo = guildInfo,
                            isShowDetail = true,
                        })
                    end
                end
            end
            serverGuildRankInfos.rankInfos = rankInfos
            serverGuildRankInfos.myGuildPaiming = rawget(msg, "myRank") or 0 --未上榜=0 没公会也是0
            --通知去刷新公会排行界面
            app:sendMsg( "NewRankController", "refreshGuildRankView",serverGuildRankInfos)
        end, {block = true})

    end
end



------------------------ request ------------------------

return ClassRef
